/*
 * Group 3, User Interface Programming II (vt11)
 * Pooya | Flora | Monica | Can
 */
package vical.undo;

/**
 * The Manager to deal with both undo and redo.
 * 
 * @author Can
 */
public class UndoManager {

    private IUndoableEvent events[];
    private int position;
    private static int maxEventsHistory = 50;
    private boolean isDebug = false;

    public UndoManager() {
        this.events = new IUndoableEvent[maxEventsHistory];
        this.position = 0;
    }

    public void registerEvent(IUndoableEvent event) {
        if (isDebug) {
            System.out.println("Before Register: " + position);
        }
        events[position++] = event;
        if (isDebug) {
            System.out.println("After Register: " + position);
        }
    }

    /**
     * Undo the appropriate edits
     */
    public void undo() {
        if (isDebug) {
            System.out.println("Before Undo: " + position);
        }
        if (position == 0) {
            return;
        }
        events[--position].unexecute();
        if (isDebug) {
            System.out.println("After Undo: " + position);
        }
    }

    /**
     * Redo the appropriate edits
     */
    public void redo() {
        if (isDebug) {
            System.out.println("Before Redo: " + position);
        }
        if (position == 0 && events[position] == null) {
            return;
        }
        events[position++].reexecute();
        if (isDebug) {
            System.out.println("After Redo: " + position);
        }
    }
}
